﻿using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;

namespace MQTT.Server.Web
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        /// <summary>
        /// 读取参数 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static IDictionary<string, string> Read(string[] args)
        {
            Dictionary<string, string> _params = new Dictionary<string, string>();
            for (var i = 0; i < args.Length; i++)
            {
                var p = args[i];
                if (p.StartsWith("--"))
                {
                    //参数
                    _params.Add(p.Replace("--", ""),
                        ((i < args.Length - 1) && !args[i + 1].StartsWith("--")) ? args[i + 1] : "TRUE");
                }
            }
            return _params;
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            var _params_ = Read(args);
            string url = _params_.ContainsKey("url") ? _params_["url"] : $"http://*:{(_params_.ContainsKey("port") ? _params_["port"] : "5002")}";

            return WebHost.CreateDefaultBuilder(args)
                    .UseUrls(url)
                    .UseStartup<Startup>();
        }
    }
}
